package com.brk.socket.server;

import com.alibaba.fastjson.JSON;
import com.brk.socket.common.CommonResult;
import com.brk.socket.common.ResultCodeEnum;
import com.brk.socket.entity.Location;
import com.brk.socket.server.websocket.WebSocketDispose;
import com.brk.socket.util.LocationInfoUtil;
import com.brk.socket.util.gps.Gps;
import com.brk.socket.util.gps.PositionUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import javafx.geometry.Pos;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author brk
 * @date 2023/7/14 14:41
 * @description
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class CustomServerHandler extends ChannelInboundHandlerAdapter {

    @Autowired
    WebSocketDispose webSocketDispose;

    /**
     * 注册时执行
     */
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
        ctx.writeAndFlush("Hello Client");
        System.out.println("--channelRegistered--" + ctx.channel().id().toString());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        // 处理接收到的消息
        // ...

        if (msg == null) {
            return;
        }
        // 获取客户端发送的消息
        ByteBuf byteBuf = (ByteBuf) msg;
        String message = byteBuf.toString(CharsetUtil.UTF_8);

        if(message.trim().length() == 0 || message.contains("Hello")) {
            //回复消息
            String response = "Hello, client!";
            ByteBuf buf = Unpooled.copiedBuffer(response.getBytes());
            ctx.writeAndFlush(buf);
            ctx.flush();
            return;
        }

        try {
            System.out.println("Received message from client: " + message);
            String code = message.substring(0, 6);
            message = message.substring(7);
            // 打印客户端发送的消息

            Location location = LocationInfoUtil.parseLocationString(message.trim());
            if(code.contains("1")) {
                log.warn("儿童报警");
                webSocketDispose.sendMessageAll(JSON.toJSONString(CommonResult.failed(ResultCodeEnum.UNSAFE, ResultCodeEnum.UNSAFE.getMessage(), location)));
            }

            webSocketDispose.sendLocation(location);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        String response = "Hello, client!";
//        ByteBuf buf = Unpooled.copiedBuffer(response.getBytes());
//        ctx.writeAndFlush(buf);
        ctx.flush();
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // 处理异常
        cause.printStackTrace();
        ctx.close();
    }


}
